package com.jingogo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jingogo.common.Result;
import com.jingogo.entity.User;
import com.jingogo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * 用户控制器
 * 
 * @author jingogo
 * @since 2024-01-01
 */
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关接口")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "客户端用户注册")
    public Result<String> register(@Valid @RequestBody User user,
                                  @RequestParam @NotBlank(message = "验证码不能为空") String verifyCode) {
        userService.register(user, verifyCode);
        return Result.success("注册成功");
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录")
    public Result<String> login(@RequestParam @NotBlank(message = "用户名不能为空") String username,
                               @RequestParam @NotBlank(message = "密码不能为空") String password) {
        String token = userService.login(username, password);
        return Result.success("登录成功", token);
    }

    @PostMapping("/sendVerifyCode")
    @Operation(summary = "发送验证码", description = "发送手机验证码")
    public Result<String> sendVerifyCode(@RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone,
                                        @RequestParam @NotBlank(message = "验证码类型不能为空") String type) {
        userService.sendVerifyCode(phone, type);
        return Result.success("验证码发送成功");
    }

    @PutMapping("/info")
    @Operation(summary = "更新用户信息", description = "更新用户资料")
    @PreAuthorize("hasRole('USER')")
    public Result<String> updateUserInfo(@Valid @RequestBody User user) {
        userService.updateUserInfo(user);
        return Result.success("用户信息更新成功");
    }

    @PostMapping("/verifyIdentity")
    @Operation(summary = "实名认证", description = "用户实名认证")
    @PreAuthorize("hasRole('USER')")
    public Result<String> verifyIdentity(@RequestParam Long userId,
                                        @RequestParam @NotBlank(message = "真实姓名不能为空") String realName,
                                        @RequestParam @NotBlank(message = "身份证号不能为空") String idCard) {
        userService.verifyIdentity(userId, realName, idCard);
        return Result.success("实名认证成功");
    }

    @PostMapping("/invite")
    @Operation(summary = "邀请用户", description = "邀请好友注册")
    @PreAuthorize("hasRole('USER')")
    public Result<String> inviteUser(@RequestParam Long inviterId,
                                    @RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone) {
        userService.inviteUser(inviterId, phone);
        return Result.success("邀请发送成功");
    }

    @GetMapping("/list")
    @Operation(summary = "用户列表", description = "分页查询用户列表")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<IPage<User>> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam(required = false) String keyword) {
        IPage<User> userPage = userService.getUserPage(pageNum, pageSize, keyword);
        return Result.success(userPage);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户信息", description = "根据ID获取用户信息")
    @PreAuthorize("hasRole('USER')")
    public Result<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @GetMapping("/checkPhone")
    @Operation(summary = "检查手机号", description = "检查手机号是否已存在")
    public Result<Boolean> checkPhone(@RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone) {
        boolean exists = userService.isPhoneExists(phone);
        return Result.success(exists);
    }

    @GetMapping("/checkUsername")
    @Operation(summary = "检查用户名", description = "检查用户名是否已存在")
    public Result<Boolean> checkUsername(@RequestParam @NotBlank(message = "用户名不能为空") String username) {
        boolean exists = userService.isUsernameExists(username);
        return Result.success(exists);
    }

    @PostMapping("/addPoints")
    @Operation(summary = "增加用户积分", description = "完成订单回调增加积分")
    @PreAuthorize("hasRole('USER')")
    public Result<String> addUserPoints(@RequestParam @NotNull(message = "用户ID不能为空") Long userId,
                                      @RequestParam @NotNull(message = "积分不能为空") Integer points) {
        userService.addUserPoints(userId, points);
        return Result.success("用户积分增加成功");
    }

} 